package com.dashu.device.config;

import com.dashu.base.enums.device.ModuleType;
import com.dashu.base.redis.JedisUtil;
import com.dashu.base.redis.RedisLiveKeyConstants;
import com.dashu.base.utils.date.TimeUtils;
import com.dashu.device.netty.server.CQNettyServer;
import com.dashu.device.netty.server.HFNettyServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.sql.*;

@Configuration
public class MyApplicationRunner implements ApplicationRunner {

    private String tableName = TimeUtils.getNowTimeDate();

    @Value(value = "${spring.datasource1.url}")
    public String url;

    @Value(value = "${spring.datasource1.username}")
    public String userName;

    @Value(value = "${spring.datasource1.password}")
    public String password;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        new Thread(() -> {
            JedisUtil.del(RedisLiveKeyConstants.ON_LINE);
            HFNettyServer.init(8076, ModuleType.YQ);
        }).start();
        new Thread(() -> {
            CQNettyServer.init(8075, ModuleType.SY);
        }).start();
    }


    @Modifying
    @Transactional
//    @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
    @Scheduled(cron = "0 0 0 * * ?")
    public @PostConstruct
    void testDynamicDdl() throws SQLException {

//        StringBuffer sql = new StringBuffer("create table if not exists " + tableName + " like mytable");
//        Query query = entityManager11.createNativeQuery(sql.toString());
//        query.executeUpdate();

        //以上是使用JPA中的方法，执行报错
        //下面是获取Session的方式，还是执行失败
        try {
            Connection connection = DriverManager.getConnection(url, userName, password);
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet rs = metaData.getTables(connection.getCatalog(), "BILL", "BILLING_FILE", new String[]{"TABLE"});
//        if (rs == null) {
            PreparedStatement stmt = connection.prepareStatement("CREATE TABLE `" + tableName + "` (\n" +
                    "  `id` bigint NOT NULL COMMENT '主键',\n" +
                    "  `name` varchar(255) DEFAULT NULL,\n" +
                    "  `iden` varchar(255) DEFAULT NULL COMMENT '身份',\n" +
                    "  `c_id` bigint DEFAULT NULL COMMENT '人员id',\n" +
                    "  `img` varchar(255) DEFAULT NULL COMMENT '抓拍',\n" +
                    "  `icon` varchar(255) DEFAULT NULL COMMENT '底图',\n" +
                    "  `uuid` varchar(255) DEFAULT NULL COMMENT 'uuid',\n" +
                    "  `capture_time` bigint DEFAULT NULL COMMENT '抓拍时间',\n" +
                    "  `compare_result` double(255,2) DEFAULT NULL COMMENT '比对结果',\n" +
                    "  `temperature` double(255,2) DEFAULT NULL COMMENT '温度',\n" +
                    "  `company_id` bigint DEFAULT NULL COMMENT '所属单位',\n" +
                    "  `department_id` bigint DEFAULT NULL COMMENT '所属年级',\n" +
                    "  `cls_id` bigint DEFAULT NULL COMMENT '所属组织',\n" +
                    "  `attendance_id` bigint DEFAULT NULL COMMENT '考勤规则',\n" +
                    "  `personal_type` varbinary(255) DEFAULT NULL COMMENT '类型',\n" +
                    "  `num` varbinary(255) DEFAULT NULL COMMENT '单位编号',\n" +
                    "  `teacher_phone` bigint DEFAULT NULL COMMENT '教师号码',\n" +
                    "  `attendance_num` bigint DEFAULT NULL COMMENT '第几次打卡',\n" +
                    "  `teacher_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '教师类型',\n" +
                    "  `status` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '状态',\n" +
                    "  `in_and_out` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '进出类型',\n" +
                    "  PRIMARY KEY (`id`)\n" +
                    ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;");
            stmt.execute();
//        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
